﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SmokinEditor
{
    public partial class Editor_Tools : Form
    {
        public Game_Editor parent;
        public Type SelectedObject = null;
        /*
        public Type[] constructor_parameters = null;
        public object[] _default_const_values = null;
        [ReadOnlyAttribute(true)]
        public object[] default_const_values
        {
            get { return _default_const_values; }
            set { _default_const_values = value; }
        }
        */
        public HashSet<SmokinAces.Game_Object> Selection = new HashSet<SmokinAces.Game_Object>();
        public Property_List TheProperties = null;
        public Editor_Tools(Game_Editor Parent)
        {
            parent = Parent;
            InitializeComponent();
            Selection temp0 = new Selection(this);
            this.Tools_Panel.Controls.Add(temp0);
            Drag_DropControl temp1 = new Drag_DropControl(typeof(SmokinAces.Object_Classes.Objects.Tree), this);
            temp1.Left = temp0.Left + temp0.Width;
            this.Tools_Panel.Controls.Add(temp1);
            Drag_DropControl temp2 = new Drag_DropControl(typeof(SmokinAces.Object_Classes.Objects.Flower), this);
            temp2.Left = temp1.Left + temp1.Width;
            this.Tools_Panel.Controls.Add(temp2);
            Drag_DropControl temp3 = new Drag_DropControl(typeof(SmokinAces.Object_Classes.Objects.Pot), this);
            temp3.Left = temp2.Left + temp2.Width;
            this.Tools_Panel.Controls.Add(temp3);
            Drag_DropControl temp4 = new Drag_DropControl(typeof(SmokinAces.Object_Classes.Objects.Bushes), this);
            temp4.Left = 0;
            temp4.Top = temp0.Top + temp0.Height;
            this.Tools_Panel.Controls.Add(temp4);
            Drag_DropControl temp5 = new Drag_DropControl(typeof(SmokinAces.Object_Classes.Objects.NPC), this);
            temp5.Left = temp4.Left + temp4.Width;
            temp5.Top = temp0.Top + temp0.Height;
            this.Tools_Panel.Controls.Add(temp5);
            Drag_DropControl temp6 = new Drag_DropControl(typeof(SmokinAces.Object_Classes.Objects.Main_Character), this);
            temp6.Left = temp5.Left + temp5.Width;
            temp6.Top = temp0.Top + temp0.Height;
            this.Tools_Panel.Controls.Add(temp6);
            Drag_DropControl temp7 = new Drag_DropControl(typeof(SmokinAces.Object_Classes.Objects.Item), this);
            temp7.Left = temp6.Left + temp6.Width;
            temp7.Top = temp0.Top + temp0.Height;
            this.Tools_Panel.Controls.Add(temp7);
            Drag_DropControl temp8 = new Drag_DropControl(typeof(SmokinAces.Object_Classes.Objects.Seams), this);
            temp8.Left = 0;
            temp8.Top = temp4.Top + temp4.Height;
            this.Tools_Panel.Controls.Add(temp8);
            Drag_DropControl temp9 = new Drag_DropControl(typeof(SmokinAces.Object_Classes.Objects.Boulder), this);
            temp9.Left = temp8.Left + temp8.Width;
            temp9.Top = temp4.Top + temp4.Height;
            this.Tools_Panel.Controls.Add(temp9);
            Drag_DropControl temp10 = new Drag_DropControl(typeof(SmokinAces.Object_Classes.Objects.Cactus), this);
            temp10.Left = temp9.Left + temp9.Width;
            temp10.Top = temp4.Top + temp4.Height;
            this.Tools_Panel.Controls.Add(temp10);
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Editor_Tools_Load(object sender, EventArgs e)
        {

        }
        protected override void OnClosed(EventArgs e)
        {
            parent.Exit();
            base.OnClosed(e);
        }

        private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Map_Creator Create = new Map_Creator(this.parent);
            Create.ShowDialog(this.parent.FormHandle);
        }

        private void PropertyChanger_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
        {
            /*
            foreach (SmokinAces.Game_Object iter in Selection)
            {
                e.ChangedItem.PropertyDescriptor.SetValue(iter, e.ChangedItem.Value);
            }*/
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            parent.saveFile();
        }

        private void loadToolStripMenuItem_Click(object sender, EventArgs e)
        {
            parent.loadFile();
        }

    }
    
    public class Custom_Property
    {
        public string Name = "GridWars";
        private Type[] _constructor_parameters = null;
        public Type[] constructor_parameters
        {
            get { return _constructor_parameters; }
            set { _constructor_parameters = value; }
        }
        private object[] _default_const_values = null;
        public object[] default_const_values
        {
            get { return _default_const_values; }
            set { _default_const_values = value; }
        }

    }

    public class Property_List : ICustomTypeDescriptor
    {
        public Custom_Property Blast;
        public Property_List(Type[] cParms, Object[] dValues, string Name)
        {
            Blast = new Custom_Property();
            Blast.default_const_values = dValues;
            Blast.constructor_parameters = cParms;
            Blast.Name = Name;
        }

        // Implementation of ICustomTypeDescriptor: 


        public String GetClassName()
        {
            return Blast.Name;
            //return TypeDescriptor.GetClassName(this, true);
        }

        public AttributeCollection GetAttributes()
        {
            return TypeDescriptor.GetAttributes(this, true);
        }

        public String GetComponentName()
        {
            return Blast.Name;
            //return TypeDescriptor.GetComponentName(this, true);
        }

        public TypeConverter GetConverter()
        {
            return TypeDescriptor.GetConverter(this, true);
        }

        public EventDescriptor GetDefaultEvent()
        {
            return TypeDescriptor.GetDefaultEvent(this, true);
        }

        public PropertyDescriptor GetDefaultProperty()
        {
            return TypeDescriptor.GetDefaultProperty(this, true);
        }

        public object GetEditor(Type editorBaseType)
        {
            return TypeDescriptor.GetEditor(this, editorBaseType, true);
        }

        public EventDescriptorCollection GetEvents(Attribute[] attributes)
        {
            return TypeDescriptor.GetEvents(this, attributes, true);
        }

        public EventDescriptorCollection GetEvents()
        {
            return TypeDescriptor.GetEvents(this, true);
        }

        public object GetPropertyOwner(PropertyDescriptor pd)
        {
            return this;
        }

        public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
        {
                     // Create a new collection object PropertyDescriptorCollection

            PropertyDescriptorCollection pds = new PropertyDescriptorCollection(null);

            // Iterate the list of employees

            for (int i = 0; i < Blast.default_const_values.Count(); i++)
            {
                GamePropertyPropertyDescriptor pd = new GamePropertyPropertyDescriptor(ref Blast, attributes, Blast.constructor_parameters[i].Name);
                pd.index = i;
                pd.defaultvalue = Blast.default_const_values[i];
                pds.Add(pd);
            }
            return pds;
        }
        public PropertyDescriptorCollection GetProperties()
        {
            return null;
        }

    }



    internal sealed class GamePropertyPropertyDescriptor : PropertyDescriptor
    {
        public Custom_Property parent = null;
        public int index = -1;
        public object defaultvalue = null;
        public GamePropertyPropertyDescriptor(ref Custom_Property myProperty, Attribute[] attrs, String Name)
            : base(Name, attrs)
         {
          parent = myProperty;
         }

        public override bool CanResetValue(object component)
        {
            return true;
        }

        public override Type ComponentType
        {
            get { return typeof(Object[]); }
        }

        public override object GetValue(object component)
        {
            return parent.default_const_values[index];
        }

        public override bool IsReadOnly
        {
            get { return false; }
        }

        public override Type PropertyType
        {
            get { return parent.constructor_parameters[index]; }
        }

        public override void ResetValue(object component)
        {
            parent.default_const_values[index] = defaultvalue;
        }

        public override void SetValue(object component, object value)
        {
            parent.default_const_values[index] = value;
        }

        public override bool ShouldSerializeValue(object component)
        {
            return true;
        }
    }
}
